package com.dai.zhenfujin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ResBdBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.my.MyInfoActivity;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.utils.UIUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import java.util.HashMap;
import java.util.Map;

/**
 * 账户密码登录
 */
public class AccountPasswordActivity extends BaseActivity implements View.OnClickListener {

    private TextView mApa_cancel, tv_login, apa_cancel;
    private TextView mApa_login;
    private LinearLayout mLinearlayout;
    private EditText mApa_phone;
    private EditText mApa_password;
    private Button mRegister_button;
    private TextView mApa_register;
    private TextView mText, mRegister_login, tv_find_password;
    public static String phone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_password);
        mApa_cancel = (TextView) findViewById(R.id.apa_cancel);
        mApa_login = (TextView) findViewById(R.id.apa_login);
        mLinearlayout = (LinearLayout) findViewById(R.id.linearlayout);
        mApa_phone = (EditText) findViewById(R.id.apa_phone);
        mApa_password = (EditText) findViewById(R.id.apa_password);
        mRegister_button = (Button) findViewById(R.id.register_button);
        mApa_register = (TextView) findViewById(R.id.apa_register);
        mRegister_login = (TextView) findViewById(R.id.register_login);
        tv_find_password = (TextView) findViewById(R.id.tv_find_password);

        //帐号密码登录
        mText = (TextView) findViewById(R.id.text);
        mRegister_button.setOnClickListener(this);
        mApa_login.setOnClickListener(this);
        tv_find_password.setOnClickListener(this);
        mApa_cancel.setOnClickListener(this);
        //快捷登录
        mApa_register.setOnClickListener(this);
        if (SPUtils.getBoolean(AccountPasswordActivity.this, "login")) {
            startActivity(new Intent(AccountPasswordActivity.this, MainActivity.class));
            finish();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //登录
            case R.id.register_button:
                login();
                break;
            //注册
            case R.id.apa_login:
                startActivity(new Intent(this, LoginActivity.class));
                break;
            //快速登录
            case R.id.apa_register:
                //startActivity(new Intent(this,RegisterActivity.class));
                // finish();
                break;
            //找回密码
            case R.id.tv_find_password:
                startActivity(new Intent(this, RetrievePasswordActivity.class));
                break;
            case R.id.apa_cancel:
                if (SPUtils.getBoolean(this, "login")) { //true

                } else {
                    startActivity(new Intent(this, MainActivity.class).putExtra("showRed",0));
                }
                finish();
                break;
        }
    }

    private void login() {
        SPUtils.put(this, Constants.TOKEN, "");
        phone = mApa_phone.getText().toString().trim();
        if (!UIUtils.isMobileNO(phone)) {
            UIUtils.showToastSafe("请输入手机号码");
            return;
        }

        if (TextUtils.isEmpty(mApa_password.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入密码");
            return;
        }


        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("mobile", phone);
        params2.put("pwd", UIUtils.Md5(mApa_password.getText().toString().trim()));
        params.put("req_bd", params2);

        HttpClient.post(this, Constants.LOGIN, params, new CallBack<ResBdBean>() {
            @Override
            public void onSuccess(ResBdBean resultBean) {
                String token = resultBean.getAccess_token();
                SPUtils.put(AccountPasswordActivity.this, Constants.TOKEN, token);
                EMClient.getInstance().chatManager().loadAllConversations();
                EMClient.getInstance().login(phone, "zfj123", new EMCallBack() {//回调
                    @Override
                    public void onSuccess() {
                        SPUtils.put(AccountPasswordActivity.this, "login", true);
                        SPUtils.put(AccountPasswordActivity.this,"regist",false);
                        Log.d("main", "登录聊天服务器成功");
                        if (resultBean.name_state == 2) { //未完善资料
                            UIUtils.showToastSafe("请先完善资料");
                            startActivity(new Intent(AccountPasswordActivity.this, MyInfoActivity.class)
                                    .putExtra("type", 1));
                            finish();
                        } else {
                            SPUtils.put(AccountPasswordActivity.this, "welcome", true);
                            SPUtils.put(AccountPasswordActivity.this, Constants.USER_MOBILE, phone);
                            startActivity(new Intent(AccountPasswordActivity.this, MainActivity.class).putExtra("showRed",0));
                            finish();
                        }
                    }

                    @Override
                    public void onProgress(int progress, String status) {

                    }

                    @Override
                    public void onError(int code, String message) {
                        if (200 == code) {
                            EMClient.getInstance().logout(true);
                            //String phone = SPUtils.getString(AccountPasswordActivity.this, Constants.USER_MOBILE);
                            EMClient.getInstance().login(phone, "zfj123", new EMCallBack() {//回调
                                @Override
                                public void onSuccess() {
                                    EMClient.getInstance().chatManager().loadAllConversations();
                                    //  SPUtils.put(getContext(), "login", true);
                                    Log.d("main", "登录聊天服务器成功");
                                    SPUtils.put(AccountPasswordActivity.this, "login", true);
                                    SPUtils.put(AccountPasswordActivity.this,"regist",false);
                                    Log.d("main", "登录聊天服务器成功");
                                    if (resultBean.name_state == 2) { //未完善资料
                                        UIUtils.showToastSafe("请先完善资料");
                                        startActivity(new Intent(AccountPasswordActivity.this, MyInfoActivity.class)
                                                .putExtra("type", 1));
                                        finish();
                                    } else {
                                        SPUtils.put(AccountPasswordActivity.this, "welcome", true);
                                        SPUtils.put(AccountPasswordActivity.this, Constants.USER_MOBILE, phone);
                                        startActivity(new Intent(AccountPasswordActivity.this, MainActivity.class).putExtra("showRed",0));
                                        finish();
                                    }
                                }

                                @Override
                                public void onProgress(int progress, String status) {

                                }

                                @Override
                                public void onError(int code, String message) {
                                    if (200 == code) {
                                        //  UIUtils.showToastSafe("用户已经登录");
                                    } else {
                                        //  UIUtils.showToastSafe("登录聊天服务器失败");
                                    }
                                    Log.d("main", "登录聊天服务器失败！");
                                    Log.d("main", code + message);
                                }
                            });

                           // UIUtils.showToastSafe("用户已经登录");
                        } else {
                            UIUtils.showToastSafe("登录聊天服务器失败");
                        }
                        Log.d("main", "登录聊天服务器失败！");
                        Log.d("main", code + message);
                    }
                });
            }
        });
    }
}
